﻿using System.ComponentModel;
using System.Text;

namespace Acl.DataCheck;

/// <summary>
/// 约束信息
/// </summary>
public class ConstraintItem
{
    /// <summary>
    /// 类别
    /// </summary>
    public string Category { get; set; }

    /// <summary>
    /// 所约束的表
    /// </summary>
    public string TableName { get; set; }

    /// <summary>
    /// 所约束的列名
    /// </summary>
    public string ColumnName { get; set; }

    /// <summary>
    /// 约束类型
    /// </summary>
    public ConstraintType Type { get; set; }

    /// <summary>
    /// 是否启用约束
    /// </summary>
    [DefaultValue(true)]
    public bool Enabled { get; set; } = true;

    /// <summary>
    /// 约束表达式
    /// </summary>
    public string Expression { get; set; }

    /// <summary>
    /// 描述信息
    /// </summary>
    public string Description { get; set; }

    /// <summary>
    /// 是否检查通过
    /// </summary>
    public bool Passed { get; set; }

    internal StringBuilder _errorMessage = default;

    /// <summary>
    /// 错误信息
    /// </summary>
    public string ErrorMessage => _errorMessage != null ? _errorMessage.ToString() : string.Empty;

    internal void ClearErrorMessage()
    {
        if (_errorMessage != null) _errorMessage.Clear();
    }

    internal void AppendLine(string message)
    {
        _errorMessage ??= new StringBuilder(512);
        _errorMessage.AppendLine(message);
    }

    /// <summary>
    ///
    /// </summary>
    /// <returns></returns>
    public override string ToString()
    {
        return $"Category={Category},TableName={TableName},ColumnName={ColumnName},Type={Type},Expression={Expression},Enabled={Enabled}";
    }
}